package com.jiulan.hearthearing.ui.my.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.jiulan.hearthearing.R;

public class PersonInfoNickNameDialog extends Dialog {
    private Context context;
    private String title, content = "";

    private ClickListenerInterface clickListenerInterface;

    public PersonInfoNickNameDialog(Context context, String title, String content) {
        super(context, R.style.custom_dialog_style);
        this.context = context;
        this.title = title;
        this.content = content;
    }

    public interface ClickListenerInterface {
        void doCancel();

        void doConfirm(String content);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置不显示键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        setContentView(R.layout.dialog_person_info_edit);
        TextView tvTitle = findViewById(R.id.tvTitle);
        EditText etName = findViewById(R.id.etName);
        TextView tvCancel = findViewById(R.id.tvCancel);
        TextView tvConfirm = findViewById(R.id.tvConfirm);
        tvTitle.setText(title);
        etName.setText(content);
        tvCancel.setOnClickListener(new clickListener());
        tvConfirm.setOnClickListener(new clickListener());
        this.setCancelable(false);
        this.setCanceledOnTouchOutside(false);
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    content = s.toString().trim();
                } catch (Exception e) {
                    content = "";
                }
            }
        });
    }

    public void setClicklistener(ClickListenerInterface clickListener) {
        clickListenerInterface = clickListener;
    }

    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tvCancel:
                    clickListenerInterface.doCancel();
                    break;
                case R.id.tvConfirm:
                    clickListenerInterface.doConfirm(content);
                    break;
            }
        }
    }
}
